`
https://leetcode.cn/problems/find-the-smallest-divisor-given-a-threshold/
`

/**
 * @param {number[]} nums
 * @param {number} threshold
 * @return {number}
 */
var smallestDivisor = function (nums, threshold) {
  // 计算选择 m 时的计算结果是否小于等于 threshold
  const check = (m) => {
    // const sum = nums.reduce((acc, cur) => acc + Math.ceil(cur / m), 0)
    // return sum <= threshold
    let sum = 0
    for (const num of nums) {
      sum += Math.ceil(num / m)
      if (sum > threshold) return false
    }
    return true
  }
  // 开区间二分
  let left = 0, right = Math.max(...nums)
  while (left + 1 < right) {
    const mid = left + ((right - left) >> 1)
    // mid 越大，sum 越小，check(i) 是倒序的
    check(mid) ? right = mid : left = mid
  }
  return right
};